|
Creating DTOs (POJOs/POCOs)
|
data class Customer(val name: String, val email: String)
👏
|
struct Customer: Decodable, Equatable, Hashable {
let name: String
let email: String
}
|
Default values for function parameters
|
fun foo(a: Int = 0, b: String = "") { ... }
|
func foo(a: Int = 0, b: String = "") { ... }
|
Filtering a list
|
val positives = list.filter { x -> x > 0 }
|
let positives = list.filter { x in x > 0 }
|
val positives = list.filter { it > 0 }
|
let positives = list.filter { $0 > 0 }
|
Checking element presence in a collection.
|
if ("john@example.com" in emailsList) { ... }
if ("jane@example.com" !in emailsList) { ... }
|
if emailsList.contains("john@example.com") { ... }
if !emailsList.contains("jane@example.com") { ... }
|
String Interpolation
|
println("Name $name")
|
print("Name \(name)")
|
Instance Checks
|
when (x) {
is Foo -> ...
is Bar -> ...
else -> ...
}
|
switch x {
case is Foo: ...
case is Bar: ...
default: ...
}
|
Traversing a map/list of pairs
|
for ((k, v) in map) {
println("$k -> $v")
}
|
for (k, v) in map.enumerated {
print("\(k) -> \(v)")
}
|
Using ranges
|
for (i in 1..100) { ... } // closed range: includes 100
for (i in 1 until 100) { ... } // half-open range: does not include 100
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
if (x in 1..10) { ... }
|
for i in 1...100 { ... } // closed range: includes 100
for i in 1..<100 { ... } // half-open range: does not include 100
for x in stride(from: 2, through: 10, by: 2) { ... }
for x in stride(from: 10, through: 0, by: -1) { ... }
if 1...10 ~= x { ... }
|
Read-only list
|
val list = listOf("a", "b", "c")
|
let list = ["a", "b", "c"]
|
Read-only map
|
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
|
let map = ["a": 1, "b": 2, "c": 3]
|
Accessing a map
|
println(map["key"])
map["key"] = value
|
print(map["key"])
map["key"] = value
|
Lazy property
|
val p: String by lazy {
// compute the string
}
|
struct A {
lazy var p: String = {
// compute the string
}()
}
|
Extension Functions
|
fun String.spaceToCamelCase() { ... }
"Convert this to camelcase".spaceToCamelCase()
|
extension String {
func spaceToCamelCase() { ... }
}
"Convert this to camelcase".spaceToCamelCase()
|
Creating a singleton
|
object Resource {
val name = "Name"
}
👏
|
struct Resource {
static let shared = Resource()
let name: String = "Name"
private init() { }
}
let resource = Resource.shared
|
Instantiate an abstract class
|
abstract class MyAbstractClass {
abstract fun doSomething()
abstract fun sleep()
}
fun main() {
val myObject = object : MyAbstractClass() {
override fun doSomething() {
// ...
}
override fun sleep() { // ...
}
}
myObject.doSomething()
}
👏
|
protocol MyAbstractClass {
func doSomething()
func sleep()
}
class AClass: MyAbstractClass {
func doSomething() { ... }
func sleep() { ... }
}
func main() {
val myObject = AClass()
myObject.doSomething()
}
|
If not null shorthand
|
val files = File("Test").listFiles()
println(files?.size)
|
let files = File("Test").listFiles()
print(files?.count)
|
If not null and else shorthand
|
val files = File("Test").listFiles()
println(files?.size ?: "empty")
|
let files = File("Test").listFiles()
print(files?.count ?? 0)
|
Executing a statement if null
|
val values = ...
val email = values["email"] ?: throw IllegalStateException("Email is missing!")
👏
|
let values = ...
guard let email = values["email"] else {
throw Error.illegalStateException("Email is missing!")
}
|
Get first item of a possibly empty collection
|
val emails = ... // might be empty
val mainEmail = emails.firstOrNull() ?: ""
|
let emails = ... // might be empty
let mainEmail = emails.first ?? ""
|
Execute if not null
|
val value = ...
value?.let {
... // execute this block if not null
}
|
let value = ...
if let value = value {
... // execute this block if not null
}
|
Map nullable value if not null
|
val value = ...
val mapped = value?.let { transformValue(it) } ?: defaultValue
// defaultValue is returned if the value or the transform result is null.
|
let value: Int? = 1
let mapped = value.flatMap { transformValue($0) } ?? defaultValue
// defaultValue is returned if the value or the transform result is null.
|
Return on when statement
|
fun transform(color: String): Int {
return when (color) {
"Red" -> 0
"Green" -> 1
"Blue" -> 2
else -> throw IllegalArgumentException("Invalid color param value")
}
}
|
enum Error: Swift.Error {
case illegalArgumentException(String)
}
func transform(color: String) throws -> Int {
switch (color) {
case "Red": return 0
case "Green": return 1
case "Blue": return 2
default: throw Error.illegalArgumentException("Invalid color param value")
}
}
|
'try/catch' expression
|
fun test() {
val result = try {
count()
} catch (e: ArithmeticException) {
throw IllegalStateException(e)
}
// Working with result
}
👏
|
indirect enum Error: Swift.Error {
case arithmeticException
case llegalStateException(Error)
}
func count() throws -> Int { ... }
func test() throws {
let result: Int
do {
result = try count()
} catch Error.arithmeticException {
throw Error.llegalStateException(.arithmeticException)
}
// Working with result
}
|
'if' expression
|
fun foo(param: Int) {
val result = if (param == 1) {
"one"
} else if (param == 2) {
"two"
} else {
"three"
}
}
👏
|
func foo(param: Int) {
let result: String
if param == 1 {
result = "one"
} else if param == 2 {
result = "two"
} else {
result = "three"
}
}
|
Builder-style usage of methods that return Unit
|
fun arrayOfMinusOnes(size: Int): IntArray {
return IntArray(size).apply { fill(-1) }
}
|
func arrayOfMinusOnes(size: Int) -> [Int] {
return [Int](repeating: -1, count: size)
}
|
Single-expression functions
|
fun theAnswer() = 42
👏
|
func theAnswer() -> Int {
42
}
|
fun theAnswer(): Int {
return 42
}
|
func theAnswer() -> Int {
return 42
}
|
fun transform(color: String): Int = when (color) {
"Red" -> 0
"Green" -> 1
"Blue" -> 2
else -> throw IllegalArgumentException("Invalid color param value")
}
👏
|
func transform(color: String) throws -> Int {
switch color {
case "Red": return 0
case "Green": return 1
case "Blue": return 2
default: throw Error.illegalArgumentException("Invalid color param value")
}
}
|
Calling multiple methods on an object instance (with)
|
class Turtle {
fun penDown()
fun penUp()
fun turn(degrees: Double)
fun forward(pixels: Double)
}
val myTurtle = Turtle()
with(myTurtle) { //draw a 100 pix square
penDown()
for (i in 1..4) {
forward(100.0)
turn(90.0)
}
penUp()
}
👏
|
class Turtle {
func penDown() {}
func penUp() {}
func turn(degrees: Double) {}
func forward(pixels: Double) {}
}
extension Turtle {
func with(_ block: (Turtle) -> Void) {
block(self)
}
}
let myTurtle = Turtle()
myTurtle.with { //draw a 100 pix square
$0.penDown()
for _ in 1...4 {
$0.forward(pixels: 100.0)
$0.turn(degrees: 90.0)
}
$0.penUp()
}
|
Configuring properties of an object (apply)
|
val myRectangle = Rectangle().apply {
length = 4
breadth = 5
color = 0xFAFAFA
}
👏
|
struct Rectangle {
var length: Int?
var breadth: Int?
var color: Int?
}
extension Rectangle {
func apply(_ block: (inout Rectangle) -> Void) -> Rectangle {
var rect = self
block(&rect)
return rect
}
}
let myRectangle = Rectangle().apply {
$0.length = 4
$0.breadth = 5
$0.color = 0xFAFAFA
}
|
Java 7's try with resources
|
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
stream.buffered().reader().use { reader ->
println(reader.readText())
}
|
|
Convenient form for a generic function that requires the generic type information
|
// public final class Gson {
// ...
// public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
// ...
inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java)
|
extension Decodable {
func from(_ json: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: json)
}
}
|
Consuming a nullable Boolean
|
val b: Boolean? = ...
if (b == true) {
...
} else {
// `b` is false or null
}
|
let b: Boolean? = ...
if b == true {
...
} else {
// `b` is false or null
}
|
Swapping two variables
|
var a = 1
var b = 2
a = b.also { b = a }
|
var a = 1
var b = 2
(a, b) = (b, a)
|